﻿using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataHandler.Encoder;
using Microsoft.Owin.Security.DataHandler.Serializer;
using Microsoft.Owin.Security.DataProtection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Owin.Security.Custom.DataHandler
{
    public class BaseSecureDataHandler<TData> : ISecureDataFormat<TData>
    {
        private readonly IDataSerializer<TData> _serializer;

        private readonly IDataProtector _protector;

        private readonly ITextEncoder _encoder;

        public BaseSecureDataHandler(IDataSerializer<TData> serializer, IDataProtector protector, ITextEncoder encoder)
        {
            this._serializer = serializer;
            this._protector = protector;
            this._encoder = encoder;
        }

        public string Protect(TData data)
        {
            byte[] numArray = this._serializer.Serialize(data);
            byte[] numArray1 = this._protector.Protect(numArray);
            return this._encoder.Encode(numArray1);
        }

        public TData Unprotect(string protectedText)
        {
            TData tDatum;
            try
            {
                if (protectedText != null)
                {
                    byte[] numArray = this._encoder.Decode(protectedText);
                    if (numArray != null)
                    {
                        byte[] numArray1 = this._protector.Unprotect(numArray);
                        if (numArray1 != null)
                        {
                            tDatum = this._serializer.Deserialize(numArray1);
                        }
                        else
                        {
                            TData tDatum1 = default(TData);
                            tDatum = tDatum1;
                        }
                    }
                    else
                    {
                        TData tDatum2 = default(TData);
                        tDatum = tDatum2;
                    }
                }
                else
                {
                    TData tDatum3 = default(TData);
                    tDatum = tDatum3;
                }
            }
            catch
            {
                TData tDatum4 = default(TData);
                tDatum = tDatum4;
            }
            return tDatum;
        }
    }
}
